{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IPython 工具使用笔记\n",
    "\n",
    "## 粘贴系统黏贴板的内容\n",
    "\n",
    "%paste 和 %cpaste 这两个魔法函数\n",
    "\n",
    "%paste会将代码格式化的黏贴，%cpaste 会多出代码提示符\n",
    "\n",
    "## 搜索并重用历史命令\n",
    "\n",
    "如: %run script.py \n",
    "\n",
    "%run  s   +  Ctrl-P 就可以向上搜索\n",
    "Ctrl-R 和bash中搜索一样使用\n",
    "\n",
    "## 输入和输出变量\n",
    "\n",
    "```text\n",
    "IPython会将最近的两个输出结果分别保存在 _ (一个下划线) 和 __ (两个下划线) 变量中.\n",
    "输入的文本被保存在名为 _iX 的变量中， X是输入行的行号\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "134217728"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 ** 27"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "134217728"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo = 'bar'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bar'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'foo'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_i4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bar'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'exec' (<ipython-input-9-c5a968a9be59>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-9-c5a968a9be59>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    exec _i4   # notebook不支持๑乛◡乛๑\u001b[0m\n\u001b[0m           ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'exec'\n"
     ]
    }
   ],
   "source": [
    "# 由于输入变量是字符串，因此可以用 Python 的 exec 关键字重新执行:\n",
    "exec _i4   # notebook不支持๑乛◡乛๑"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 记录输入和输出\n",
    "\n",
    "执行 %logstart 即可开始记录日志，其他命令 %logoff %logon %logstate 和 %logstop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 交互式调试器\n",
    "\n",
    "IPython 的调试器增强了 pdb, 如 Tab 键自动完成、语法高亮、为异常跟踪的每一条信息添加上下文参考等；调试代码的最佳时机之一就是错误刚刚发生那会儿，这时候输入 %debug命令， 将会调用哪个 \"事后\" 调试器，并直接跳转到引发异常的那个栈帧. 输入 u 和 d 即可以在栈跟踪的各个级别之间切换。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在执行代码之前打开调试器\n",
    "\n",
    "%run -d srcipt.py\n",
    "\n",
    "必须输入 s(或 step) 才可以进入脚本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
